package com.qf.springboot.realm;


import com.qf.springboot.config.ShiroConfig;
import com.qf.springboot.pojo.Permission;
import com.qf.springboot.pojo.Role;
import com.qf.springboot.pojo.User;
import com.qf.springboot.serivce.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

/**
 * @Auther: xiaobobo
 * @Date: 2020/2/26 14:51
 * @Description:这个就是shiro和数据库交互的桥梁
 */
public class MyRealm extends AuthorizingRealm {
    private Logger logger= LoggerFactory.getLogger(ShiroConfig.class);
    @Autowired
    private UserService userService;

    @Override
    public String getName() {
        return "MyRealm";
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //第一步：获取用户名
        String userName= (String) authenticationToken.getPrincipal();
        User user =userService.toLogin(userName);
        //通过用户名查询用户对象
        if (user==null){
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                user,
                user.getPassword()
                ,getName());
        logger.info("用户名"+user.getUsername());
        logger.info("密码"+user.getPassword());
        return simpleAuthenticationInfo;
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //第一步：先获取用户名
        User user= (User) principalCollection.getPrimaryPrincipal();
        //获取前端输入的用户名
        String username = user.getUsername();
        ////根据前端输入的用户名查询数据库中对应的记录
        User byUsername = userService.findByUsername(username);

        //如果数据库中有该用户名对应的记录，就进行授权操作
        if (byUsername !=null)
        {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            logger.info("这里是授权、这里执行了.........");
            Set<String> perms=new HashSet<>();
            Set<String> roles=new HashSet<>();
            //获取user的Role的set集合
            Set<Role> roles1 = user.getRoles();
            //遍历集合
            for (Role role : roles1) {
                //将每一个role的name装进集合里面
                roles.add(role.getName());
                ////获取每一个Role的permission的set集合
                Set<Permission> permissions = role.getPermissions();
                for (Permission permission : permissions) {
                    //将每一个permission的name装进集合里面
                    perms.add(permission.getName());
                }
                //为用户授权
                info.addStringPermissions(perms);

            }
            //为用户授予角色
            info.addStringPermissions(roles);
            return info;

        }else {
            return null;
        }
    }
}
